//
//  CYCarRiskRewardTVC.m
//  CreditAddressBookEE
//
//  Created by LE on 16/1/26.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYCarRiskRewardTVC.h"
#import "CYSelectAmountView.h"
#import "MBProgressHUD+LE.h"
#import "LEMDLib.h"
#import "CYDiscoveryHttpTool.h"
#import "CYCompanyUserTool.h"
#import "CYCompanyAccount.h"
#import "CYJumpsTool.h"
#import "LEDIYHeader.h"
#import "CYCarCompanyAccount.h"
//#import "CYHelpInfoView.h"
#import "LEAlertView+LE.h"
#import "UIInfomationView.h"
#import "MJExtension.h"
#import "CYCompanyRechargeVC.h"
#import "TOWebViewController+LE.h"
#import "UIViewController+LEBackButtonHandler.h"
#import "LEPresentingAnimatorTool.h"
#import "LEWebViewController+LE.h"
#import "CYLoginHttpTool.h"

@interface CYCarRiskRewardTVC ()

@property (weak, nonatomic) IBOutlet UILabel *yeLabel;
@property (weak, nonatomic) IBOutlet UILabel *bzjLabel;
@property (weak, nonatomic) IBOutlet UILabel *kyedlabel;
@property (weak, nonatomic) IBOutlet UIButton *yeHelpBtn;
@property (weak, nonatomic) IBOutlet UIButton *bzjHelpBtn;
@property (weak, nonatomic) IBOutlet UIButton *kyedHelpBtn;
@property (weak, nonatomic) IBOutlet UIView *zrjeSelView;

@property(nonatomic, weak) LEAlertView *alertView;
@property(nonatomic, assign) BOOL isRefresh;
@property(nonatomic, strong) UIViewController *alertViewController;
@end

@implementation CYCarRiskRewardTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"账户管理";
    [self setupSelectAmountView];
    [self setupRefresh];
}

- (void)setupSelectAmountView {
    CYSelectAmountView *sel = [CYSelectAmountView loadFromNibUsingClassName];
    [self.zrjeSelView addSubview:sel];
    [sel autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
    [sel commitButtonClick:^(CGFloat amount) {
        if (amount > 0) {
            NSInteger account = self.yeLabel.text.integerValue;
            if (amount >= 5000 && account >= amount) {
                CYCarCompanyAccount *carAccount = [CYCarCompanyAccount new];
                carAccount.balance = @(amount);
                //[wself loadCompanyAccountRecharge:carAccount];
            } else {
                [MBProgressHUD showError:@"账户余额不足"];
            }
        } else {
            [MBProgressHUD showError:@"输入的金额不正确"];
        }
    }];
}

//- (IBAction)yeHelpBtn:(UIButton *)sender {
//    CYHelpInfoView *help = [CYHelpInfoView loadFromNibUsingClassName];
//    help.title.text = @"账户余额";
//    help.content =@"        用户目前账户里面可用的资金总额。";
//    [help.cancelBtn addTarget:self action:@selector(closeAlertView) forControlEvents:UIControlEventTouchUpInside];
////    self.alertView = [LEAlertView showCustomDialogView:help showInView:nil];
//    UIViewController *helpController = [[UIViewController alloc] init];
//    helpController.view = help;
//    [LEPresentingAnimatorTool presentingAnimatorWithController:helpController showIn:self completion:nil];
//    self.alertViewController = helpController;
//}
//- (IBAction)bzjHelpBtn:(UIButton *)sender {
//    CYHelpInfoView *help = [CYHelpInfoView loadFromNibUsingClassName];
//    help.title.text = @"风险赏金-保证金";
//    help.content = @"       用户可以将余额转入保证金，该保证金的额度是每台租赁车辆能设置的最高风险赏金的额度。\n        例:如果用户缴纳了1万元保证金，则其每台租赁车辆所能设置的最高风险赏金额度为1万元。";
//    [help.cancelBtn addTarget:self action:@selector(closeAlertView) forControlEvents:UIControlEventTouchUpInside];
////    self.alertView = [LEAlertView showCustomDialogView:help showInView:nil];
//    UIViewController *helpController = [[UIViewController alloc] init];
//    helpController.view = help;
//    [LEPresentingAnimatorTool presentingAnimatorWithController:helpController showIn:self completion:nil];
//    self.alertViewController = helpController;
//}
//- (IBAction)kyedHelpBtn:(UIButton *)sender {
//    CYHelpInfoView *help = [CYHelpInfoView loadFromNibUsingClassName];
//    help.title.text = @"风险赏金-可用额度";
//    help.content =@"        该额度为保证金额度的10倍。用户发布租赁车辆时，可以为每台车辆都设置一个风险悬赏金的额度，此额度将从风险赏金可用额度中扣除。\n        即:用户缴纳保证金1万元，获得10万元可用额度，如果用户为每台租赁车辆设置1万元的风险赏金，则该用户一共可以发布10台租赁车辆信息。";
//    [help.cancelBtn addTarget:self action:@selector(closeAlertView) forControlEvents:UIControlEventTouchUpInside];
////    self.alertView = [LEAlertView showCustomDialogView:help showInView:nil];
//    UIViewController *helpController = [[UIViewController alloc] init];
//    helpController.view = help;
//    [LEPresentingAnimatorTool presentingAnimatorWithController:helpController showIn:self completion:nil];
//    self.alertViewController = helpController;
//}
//- (IBAction)jnbzjHelpBtn:(UIButton *)sender {
//    CYHelpInfoView *help = [CYHelpInfoView loadFromNibUsingClassName];
//    help.title.text = @"保证金机制";
//    help.content =@"        用户缴纳保证金之后，所发布的车辆便进入保证机制。当车辆发生风险时，由风险控制方向车辆发布者提起保证金申请，车辆发布者与对方达成一致后，系统将从车辆发布者余额账户中扣除相应金额支付至对方账户，余额不足的将从保证金帐户中划扣。";
//    [help.cancelBtn addTarget:self action:@selector(closeAlertView) forControlEvents:UIControlEventTouchUpInside];
////    self.alertView = [LEAlertView showCustomDialogView:help showInView:self.navigationController.view];
//    UIViewController *helpController = [[UIViewController alloc] init];
//    helpController.view = help;
//    [LEPresentingAnimatorTool presentingAnimatorWithController:helpController showIn:self completion:nil];
//    self.alertViewController = helpController;
//}

- (void)closeAlertView {
//    [self.alertView close];
    [self.alertViewController dismissViewControllerAnimated:YES completion:nil];
}

/*!
 *  @brief  设置下啦刷新
 */
- (void)setupRefresh {
    CYWeakObject(wself, self);
    LEDIYHeader *header = [LEDIYHeader headerWithRefreshingBlock:^{
        [wself refreshBalance];
    }];
    self.tableView.mj_header = header;
    [header beginRefreshing];
}

- (void)refreshBalance {
    CYCompanyUserModel *user = [CYCompanyUserTool companyUser];
    if (user) {
        //加载余额
        //[self loadBalance];
        //加载额度与保证金
        //[self loadCompanyCarReward];
    } else {
        [self.tableView.mj_header endRefreshing];
        self.yeLabel.text = @"--";
        [self.navigationController popToRootViewControllerAnimated:NO];
        [CYJumpsTool jumpToCompanyLogin:self];
    }
}

//#pragma mark - 网络请求
///**
// *  加载余额
// */
//- (void)loadBalance {
//    CYUser *user = [CYLoginHttpTool userData].user;
//    if (user) {
////        [MBProgressHUD showMessage:nil toView:self.view];
//        [CYDiscoveryHttpTool getCompanyAccountBalanceWithParam:@{@"companyId":user.userId, @"userId": user.userId, @"token": user.token} success:^(CYCompanyAccount *userAccount) {
//            self.yeLabel.text = [NSString stringWithFormat:@"%@", userAccount.balance];
//            [self.tableView.mj_header endRefreshing];
//        } failure:^(NSString *message, BOOL isLogin) {
//            [MBProgressHUD showError:message];
//            [self.tableView.mj_header endRefreshing];
//            if (!isLogin) {
//                [self.navigationController popToRootViewControllerAnimated:NO];
//                [CYCompanyUserTool logoutWithController:self];
//            } else {
//                
//            }
//            self.yeLabel.text = @"--";
//        }];
//    } else {
//        [self.tableView.mj_header endRefreshing];
//        self.yeLabel.text = @"--";
//        [self.navigationController popToRootViewControllerAnimated:NO];
//        [CYJumpsTool jumpToCompanyLogin:self];
//    }
//}
/*!
 *  @brief  加载租车风险金
 */
//- (void)loadCarUserAccount {
//    CYUser *user = [CYLoginHttpTool userData].user;
//    if (user) {
//        [CYDiscoveryHttpTool getCarCompanyAccountByCompanyIdWithParam:@{@"companyId": user.userId, @"userId": user.userId, @"token": user.token} success:^(CYCarCompanyAccount *account) {
//            self.bzjLabel.text = [NSString stringWithFormat:@"%@", account.balance];
//        } failure:^(NSString *message, BOOL isLogin) {
//            self.bzjLabel.text = @"--";
//            if (!isLogin) {
//                [self.navigationController popToRootViewControllerAnimated:NO];
//                [CYCompanyUserTool logoutWithController:self];
//            }
//        }];
//    }
//}
/*!
 *  @brief  加载额度与风险金账户
 */
//- (void)loadCompanyCarReward {
//    CYCompanyUserModel *user = [CYCompanyUserTool companyUser];
//    [CYDiscoveryHttpTool getCompanyCarRewardWithParam:@{@"companyId": user.companyId, @"userId": user.companyUserId, @"token": user.token} success:^(NSNumber *account, NSNumber *riskAccount) {
//        [self.tableView.mj_header endRefreshing];
//        self.kyedlabel.text = [NSString stringWithFormat:@"%@", riskAccount];
//        self.bzjLabel.text = [NSString stringWithFormat:@"%@", account];
//    } failure:^(NSString *message, BOOL isLogin) {
//        [self.tableView.mj_header endRefreshing];
//        [MBProgressHUD showError:message];
//        self.bzjLabel.text = @"--";
//        self.kyedlabel.text = @"--";
//        if (!isLogin) {
//            [self.navigationController popToRootViewControllerAnimated:NO];
//            [CYCompanyUserTool logoutWithController:self];
//        }
//    }];
//}
/*!
 *  @brief  账户转入风险金
 */
//- (void)loadCompanyAccountRecharge:(CYCarCompanyAccount *)account {
//    CYUser *user = [CYLoginHttpTool userData].user;
//    account.companyId = user.userId;
//    NSMutableDictionary *param = account.keyValues;
//    param[@"userId"] = user.userId;
//    param[@"token"] = user.token;
//    [CYDiscoveryHttpTool carCompanyAccountRechargeWithParam:param success:^(NSString *message) {
//        [MBProgressHUD showSuccess:message];
//        //刷新
//        [self.tableView.mj_header beginRefreshing];
//    } failure:^(NSString *message, BOOL isLogin) {
//        [MBProgressHUD showError:message];
//        if (!isLogin) {
//            [self.navigationController popToRootViewControllerAnimated:NO];
//            [CYCompanyUserTool logoutWithController:self];
//        }
//    }];
//}
/*!
 *  @brief  企业充值
 */
//- (void)companyRecharge:(CGFloat)balance {
//    CYCompanyUserModel *user = [CYCompanyUserTool companyUser];
//    [CYDiscoveryHttpTool companyRechargeWithParam:@{@"userId": user.companyUserId, @"token": user.token, @"account": @(balance)} success:^(NSString *html) {
//        CYLog(@"html:%@", html);
//    } failure:^(NSString *message, BOOL isLogin) {
//        [MBProgressHUD showError:message];
//        if (!isLogin) {
//            [self.navigationController popToRootViewControllerAnimated:NO];
//            [CYCompanyUserTool logoutWithController:self];
//        }
//    }];
//}

//- (void)setupTipView {
//    UILabel *tip = [[UILabel alloc] init];
//    tip.text = @"网络错误，请稍后再试";
//    tip.font = [UIFont boldSystemFontOfSize:20];
//    tip.textColor = [UIColor grayColor];
//    [self.view addSubview:tip];
//    [tip autoCenterInSuperview];
//    self.tip = tip;
//    tip.hidden = YES;
//}

//#pragma mark - Table view data source
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    if (indexPath.section == 1) {
//        CYWeakObject(wself, self);
//        [UIInfomationView showAlertViewWithTitle:@"请输入充值金额" message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] style:UIAlertViewStylePlainTextInput keyboardType:UIKeyboardTypeNumberPad clickAtIndex:^(UIAlertView *alertView, NSInteger buttonIndex) {
//            UITextField *tf = [alertView textFieldAtIndex:0];
//            [tf resignFirstResponder];
//            if (buttonIndex == 1) {
//                CYLog(@"tf: %@", tf.text);
//                CGFloat amount = tf.text.doubleValue;
//                if (amount > 0) {
//                    [CYGFBTool rechargeAmount:tf.text userType:CYUserTypePerson];
//                    [self companyRecharge:amount];
//                    CYCompanyUserModel *user = [CYCompanyUserTool companyUser];
//                    CYCompanyRechargeVC *recharge = [[CYCompanyRechargeVC alloc] init];
//                    recharge.param = @{@"userId": user.companyUserId, @"token": user.token, @"account": @(amount)};
//                    [self.navigationController pushViewController:recharge animated:YES];
                    
//                    NSString *url = [CYURL shared].companyRecharge;
//                    TOWebViewController *web = [TOWebViewController createWebViewControllerWithURL:url param:@{@"userId": user.companyUserId, @"token": user.token, @"account": @(amount)}];
                    //设置滑动返回
//                    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
//                    LEWebViewController *web = [LEWebViewController createWebViewControllerWithURL:url param:@{@"userId": user.companyUserId, @"token": user.token, @"account": @(amount)}];
//                    [self.navigationController pushViewController:web animated:YES];
//                } else {
//                    [MBProgressHUD showError:@"输入金额有误"];
//                }
//            }
//        }];
//    }
//}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return .1f;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    return 10;
//}

//#pragma mark - 重写
//- (void)viewDidAppear:(BOOL)animated {
//    [super viewDidAppear:animated];
//    if (self.isRefresh) {
//        [self.tableView.mj_header beginRefreshing];
//    } else {
//        self.isRefresh = YES;
//    }
//    //设置滑动返回
//    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
//}

//#pragma mark -
//- (BOOL)navigationShouldPopOnBackButton {
//    self.isRefresh = NO;
//    return YES;
//}
//
//- (void)dealloc {
//    CYLog(@"账户管理中心销毁");
//}
@end

